#!/bin/sh
set -e

SCRIPTPATH=$(dirname "$0")

cd "$SCRIPTPATH"

set -a
if [ -f ../../.env ]; then . ../../.env; fi
export DATABASE_URL="${PG_EXAMPLE_DATABASE_URL}"
if [ -z "$BACKEND" ]; then
    export BACKEND="postgres"
fi
set +a

if [ -z "${BACKEND##*postgres*}" ]; then
    find . -maxdepth 1 -mindepth 1 -type d | while read -r dir; do
        cd "$dir"

        if [ -d "migrations" ]; then
            ../../../bin/diesel database reset
        fi

        cargo test
        cd ..
    done
fi
